<template>
  <!-- 问题澄清表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="700px"
    draggable
    @close="closeDialog"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      style="margin-top: -20px"
    >
      <el-row>
        <el-col :span="24"> <h4>理由</h4> </el-col>
        <el-col :span="24">
          <el-form-item>
            <el-input
              :autosize="{ minRows: 6, maxRows: 6 }"
              v-if="!props.isReadonly"
              v-model="formData.reasonContent"
              maxlength="255"
              style="width: 850px"
              disabled
              placeholder="请输入澄清理由内容"
              show-word-limit
              type="textarea"
            />
            <span v-else>{{ formData.reasonContent }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-top: -20px">
          <h4>是否认可</h4>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="evaluationResult">
            <el-radio-group
              v-model="formData.evaluationResult"
              v-if="!props.isReadonly"
              @change="changAgreeWith"
            >
              <el-radio :label="1">认可</el-radio>
              <el-radio :label="0">不认可</el-radio>
            </el-radio-group>

            <span v-else>{{ formData.evaluationResult }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="formData.evaluationResult == 0">
          <el-form-item prop="evaluationResultContent">
            <el-input
              :autosize="{ minRows: 6, maxRows: 6 }"
              v-if="!props.isReadonly"
              v-model="formData.evaluationResultContent"
              maxlength="255"
              :rules="
                formData.evaluationResult == 0
                  ? rules.evaluationResultContent
                  : ''
              "
              style="width: 850px"
              placeholder="请输入意见"
              show-word-limit
              type="textarea"
            />
            <span v-else>{{ formData.evaluationResultContent }}</span>
          </el-form-item>
        </el-col>
        <div
          style="
            margin-top: 20px;
            width: 100%;
            max-height: 200px;
            overflow-y: auto;
          "
          id="table"
        >
          <CommonUploadFile
            :isRead="props.isReadonly"
            :files="formData.assetPhotos"
            @update-files="updateFormFiles"
          />
        </div>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "QMS8DPROBLEMCONFIRMATIONEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible2"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  agreeWith: [],
  reasonCode: [],
  reasonContent: [
    { required: true, message: "请填写理由", trigger: "blur" },
    { min: 1, max: 1000, message: "长度在 1 到 1000 个字符", trigger: "blur" },
  ],
  evaluationResult: [],
  evaluationResultUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  evaluationResultUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  evaluationResultContent: [
    { required: true, message: "请填写意见", trigger: "blur" },
    { min: 1, max: 1000, message: "长度在 1 到 1000 个字符", trigger: "blur" },
  ],
  version: [],
  remarks: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  createTime: [],
  createdUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  createdUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  lastUpdateTime: [],
  lastUpdatedUserId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  lastUpdatedUserName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  enabled: [],
  deleted: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  agreeWith: 0,
  reasonCode: 0,
  reasonContent: "",
  evaluationResult: 1,
  evaluationResultUserId: "",
  evaluationResultUserName: "",
  evaluationResultContent: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  assetPhotos: [],
  approvalOpinion: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        supplyId,
        supplyCode,
        supplyName,
        agreeWith,
        reasonCode,
        reasonContent,
        evaluationResult,
        evaluationResultUserId,
        evaluationResultUserName,
        evaluationResultContent,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        assetPhotos,
        approvalOpinion,
      } = newVal;
      Object.assign(formData, {
        id,
        qualityExceptionId,
        qualityExceptionCode,
        supplyId,
        supplyCode,
        supplyName,
        agreeWith: agreeWith ? agreeWith : 1,
        reasonCode: reasonCode ? reasonCode : 1,
        reasonContent,
        evaluationResult: evaluationResult ? evaluationResult : 1,
        evaluationResultUserId,
        evaluationResultUserName,
        evaluationResultContent,
        approvalOpinion,
      });
      console.log("澄清理由", formData);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible2", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.agreeWith = 1;
  formData.reasonCode = 0;
  formData.reasonContent = "";
  formData.evaluationResult = 1;
  formData.evaluationResultUserId = "";
  formData.evaluationResultUserName = "";
  formData.evaluationResultContent = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.assetPhotos = [];
  formData.approvalOpinion = "";
}

const loading = ref(false);

/** 问题澄清保存提交 */
function handleSubmit() {
  console.log(formData);

  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "澄清" : "澄清";
      await request({
        url: "/qms/q8dissueclarification/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

const changAgreeWith = () => {
  console.log(formData.agreeWith);
};
// 图片处理
function updateFormFiles(val: []) {
  formData.assetPhotos = val;
}
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible2", false);
});
</script>
